package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.BaseResp;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "删除企业客户标签", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "tag_id", type = Type.String, description = "标签的id列表", required = false),
        @Parameter(name = "group_id", type = Type.String, description = "标签组的id列表", required = false),
        @Parameter(name = "agentid", type = Type.Integer, description = "授权方安装的应用agentid。仅旧的第三方多应用套件需要填此参数", required = false),
})
@Component
public class DeleteCorpTag implements Action {

    @Autowired
    private ExternalContactService externalContactService;

    @Override
    public Response execute(Request request) throws Exception {

        String tag_id = request.getString("tag_id");
        String group_id = request.getString("group_id");
        Integer agentid = request.getInteger("agentid");

        HashMap<String, Object> params = new HashMap<String, Object>();
        if (group_id == null && tag_id == null) {
            return Response.fail("参数错误！");
        }
        if (group_id != null) {
            String[] groupIdSplit = group_id.split(",");
            params.put("group_id", groupIdSplit);
        }
        if (tag_id != null) {
            String[] tagSplit = tag_id.split(",");
            params.put("tag_id", tagSplit);
        }
        if (agentid != null) {
            params.put("agentid", agentid);
        }

        BaseResp rsp = externalContactService.deleteCorpTag(
                request.getString("access_token"), params);
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success();
    }
}
